﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using FirmaTransportowa.ViewsInterfaces;
using System.ComponentModel;

namespace FirmaTransportowa
{
    public partial class CashierPanel : Form
    {
        private int cashierId;

        public event Action ObjectSelected;
        public event Action DoubleClick;
        public event Action NewPackage;
        public event Action Delete;

        public CashierPanel(int id)
        {
            this.cashierId = id;
            InitializeComponent();
            dataGridView1.SelectionChanged += OnDataGridViewSelectedIndexChanged;
            dataGridView1.DoubleClick += OnDataGridViewDoubleClick;
            buttonNewPackage.Click += OnNewPackage;
            buttonEditPackage.Click += OnDataGridViewDoubleClick;
            buttonDelete.Click += OnDelete;
        }

        public IList<Package> objectsList
        {
            get { return dataGridView1.DataSource as IList<Package>; }
        }

        public int selectedId
        {
            get
            {
                if (dataGridView1.SelectedRows.Count > 0 && dataGridView1.SelectedRows[0].Cells[0].Value != null)
                {
                    int i = -1;
                    Int32.TryParse(dataGridView1.SelectedRows[0].Cells[0].Value.ToString(), out i);

                    return i;
                }
                else
                {
                    return -1;
                }
            }
        }

        private void showDataTable()
        {
            dataGridView1.Columns[0].HeaderText = "ID";
            dataGridView1.Columns[1].HeaderText = "Status";
            dataGridView1.Columns[2].HeaderText = "Data dodania";
            dataGridView1.Columns[3].HeaderText = "Waga";
            dataGridView1.Columns[4].HeaderText = "Miejscowość startowa";
            dataGridView1.Columns[5].HeaderText = "Miejscowość docelowa";
            dataGridView1.Columns[0].Width = 100;
            dataGridView1.Columns[1].Width = 70;
            dataGridView1.Columns[2].Width = 65;
            dataGridView1.Columns[3].Width = 40;
            dataGridView1.Columns[4].Width = 135;
            dataGridView1.Columns[5].Width = 135;
        }

        public void LoadObjects(BindingSource bindingSource)
        {
            dataGridView1.DataSource = bindingSource;
            showDataTable();
        }

        public void LoadObject(Package obj)
        {

        }

        private void OnDataGridViewSelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.ObjectSelected != null)
            {
                this.ObjectSelected();
            }
        }

        private void OnDataGridViewDoubleClick(object sender, EventArgs e)
        {
            if (this.DoubleClick != null)
                this.DoubleClick();
        }

        private void OnNewPackage(object sender, EventArgs e)
        {
            if (this.NewPackage != null)
                this.NewPackage();
        }

        private void OnDelete(object sender, EventArgs e)
        {
            if (this.Delete != null)
                this.Delete();
        }

        private void PackagesForm_Load(object sender, EventArgs e)
        {

        }

        private void logout_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            panelLogout();
        }

        private void CashierPanel_FormClosed(object sender, FormClosedEventArgs e)
        {
            panelLogout();
        }

        private void panelLogout()
        {
            this.Dispose();

            LoginPresenter.showLoginView();
        }

        private void CashierPanel_HelpButtonClicked(object sender, System.ComponentModel.CancelEventArgs e)
        {
            HelpUtils.showHelp(this, 3);
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.F1)
            {
                HelpUtils.showHelp(this, 3);
                return true; 
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }
    }
}
